﻿using System;
using System.Linq;
using MVCBootstrap.MongoDB.Web.Security;
using NUnit.Framework;
using MVCBootstrap.MongoDB;
using MongoDB.Driver;
using System.Web.Mvc;

namespace MVCBootstrap.Tests.MongoDB {

	public class RoleTests {
		private static MongoDBRoleProvider provider;
		private const String server = "mongodb://localhost";
		private const String database = "mvcbootstraptest";

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public static void TestInitialize() {
			DependencyResolver.SetResolver(new TestResolver());
			provider = new MongoDBRoleProvider();
			provider.Initialize("MongoProvider", RoleTestHelper.GetSettings("MongoProvider"));
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			// Let's clear the database!
			new MongoClient(server).GetServer().GetDatabase(database).Drop();
		}
		#endregion

		[Test]
		public void CreateAndGet() {
			String roleName = "My role";

			provider.CreateRole(roleName);

			Assert.AreEqual(true, provider.RoleExists(roleName));

			provider.CreateRole("Second role");

			Assert.AreEqual(2, provider.GetAllRoles().Count());
		}

		[Test]
		public void Delete() {
			String roleName = "To be deleted";

			provider.CreateRole(roleName);

			Assert.AreEqual(true, provider.RoleExists(roleName));

			provider.DeleteRole(roleName, false);

			Assert.AreEqual(false, provider.RoleExists(roleName));
		}
	}
}